// components/select_modal/select_modal.js
const http=require('../../utils/http')
const utils=require('../../utils/util')
Component({

  /**
   * Component properties
   */
  properties: {

  },


  /**
   * Component initial data
   */
  data: {
    animationData: undefined,
    showModalStatus: false
  },

  
  /**
   * Component methods
   */
  methods: {

    callback(){
      // console.log("调用组件方法")
       this.triggerEvent('callback','refresh_avartal')
    },

    close_gallery(){
       this.hideModal()
    },

    open_gallery(){
      var that=this;
      wx.chooseMedia({
        count: 1,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
         that.hideModal()
         wx.showLoading({
           title: '请稍等...',
         })
           let file_path=res.tempFiles[0].tempFilePath
           that.upload(file_path)
        }
      })
    },

      //打开相机
      open_camera(){
        var that=this;
        wx.chooseMedia({
          count: 1,
          mediaType: ['image'],
          sourceType: ['camera'],
          camera: 'back',
          success(res) {
            that.hideModal()
            wx.showLoading({
              title: '请稍等...',
            })
              let file_path=res.tempFiles[0].tempFilePath
              that.upload(file_path)
          }
        })
  },

  upload(file_path){
    http.uploadFile('api/pxs/appbss/centre/uploadPhoto','file',file_path)
    .then((res)=>{
       wx.hideLoading().then(()=>{
          wx.showToast({
            title: res['msg'],
            icon:'none'
          })
       })
       getApp().globalData.staff_picture=res['data']
       this.callback()
    })
    .catch((err)=>{
      wx.hideLoading()
      wx.showToast({
        title: err,
        icon:'none'
      })
    })
  },

 //点击我显示底部弹出框
 clickme: function () {
   this.showModal();
 },

//显示对话框
showModal: function () {
  // 显示遮罩层
  var animation = wx.createAnimation({
    duration: 1000,
    timingFunction: "linear",
    delay: 0
  })
  this.animation = animation
  animation.translateY(300).step()
  this.setData({
    animationData: animation.export(),
    showModalStatus: true
  })
  setTimeout(function () {
    animation.translateY(0).step()
    this.setData({
      animationData: animation.export()
    })
  }.bind(this), 200)
},


//隐藏对话框
hideModal () {
  // 隐藏遮罩层
  var animation = wx.createAnimation({
    duration: 200,
    timingFunction: "linear",
    delay: 0
  })
  this.animation = animation
  animation.translateY(300).step()
  this.setData({
    animationData: animation.export(),
  })
  setTimeout(function () {
    animation.translateY(0).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: false
    })
  }.bind(this), 200)
},
  }
})